package com.yotoodesign.dianba.fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.adapter.SingerAdapter;
import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.bean.BaseData;
import com.yotoodesign.dianba.bean.Singer;
import com.yotoodesign.dianba.custom.pull2refresh.PullToRefreshBase.OnRefreshListener;
import com.yotoodesign.dianba.custom.pull2refresh.PullToRefreshGridView;
import com.yotoodesign.dianba.ui.MainActivity;
import com.yotoodesign.dianba.util.Constant;
import com.yotoodesign.dianba.util.DataUtils;
import com.yotoodesign.dianba.util.SingerService;


public class SingerFragment extends BaseFragment {

	private SingerSongFragment singerSongFragment;
	private String title ;
	
	private PullToRefreshGridView gvSingers;
	private SingerAdapter singerAdapter;
	private Handler mHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mHandler = new Handler();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.singer_framgment, container,false);
		TextView tvTitle = (TextView)view.findViewById(R.id.mainTitle);
		ImageButton back = (ImageButton)view.findViewById(R.id.commonBack);
		back.setOnClickListener(new OnClickListener() {
			FragmentManager fm = getActivity().getSupportFragmentManager();
			@Override
			public void onClick(View v) {
				fm.popBackStack();
			}
		});
		
		PullToRefreshGridView singers = (PullToRefreshGridView)view.findViewById(R.id.gvSingers);
		GridView mGridView = singers.getRefreshableView();
		mGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Singer singer = (Singer)getDataList().get(position);
				
				addSingerSongFragment(Constant.TAG_SINGER_SONG,singer,
						DataUtils.loadSingerSongs(singer));
			}
		});
		//设置属性
		tvTitle.setText(title);
		if(this.getDataList()!=null){
			singerAdapter = new SingerAdapter(getActivity());
			singerAdapter.setSingerList(getDataList());
			mGridView.setAdapter(singerAdapter);
		}
		
		gvSingers = (PullToRefreshGridView)view.findViewById(R.id.gvSingers);
		gvSingers.setOnRefreshListener(new OnRefreshListener(){
			@Override
			public void onRefresh() {
				new GetDataTask().execute();
			}
			
		});
		
		GlobalApplication.getMainActivity().showPinYinSearch(View.VISIBLE);
		
		return view;
	}
	
	public void setTitle(String title){
		this.title = title;
	}
	
	//添加SingerSongFragment
	private void addSingerSongFragment(String tabId,Singer singer,ArrayList<BaseData> songList){
		FragmentManager fm = getActivity().getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		//先detach主Fragment
		ft.detach(this);
		singerSongFragment = (SingerSongFragment)fm.findFragmentByTag(tabId);
		if(singerSongFragment==null){
			try {
				singerSongFragment = new SingerSongFragment();
				ft.add(R.id.realtabcontent,singerSongFragment, tabId);
				//添加到后退堆栈里
				ft.addToBackStack(tabId);
			} catch (Exception e) {
				Log.i(Constant.DIANBA,"add to back stack error");
			}						
		}else{
			ft.attach(singerSongFragment);						
		}
		singerSongFragment.setSinger(singer);
		singerSongFragment.setTitle(singer.getName());
		singerSongFragment.setDataList(songList);
		ft.commit();
		
		MainActivity mainActivity = (MainActivity)this.getActivity();
		mainActivity.setCurrentFragment(singerSongFragment);
	}

	@Override
	public void onResume() {
		super.onResume();
		MainActivity mainActivity = (MainActivity)this.getActivity();
	}

	@Override
	public void updateListView() {
		singerAdapter.setSingerList(getDataList());
		singerAdapter.notifyDataSetChanged();
	}

	
	//加载更多数据
	private void loadMore(){
		Log.i(Constant.DIANBA,"load more..........................");
		ArrayList<BaseData> singerList = new ArrayList<BaseData>();
		
		List singers = SingerService.getInstance().next();
		for(Iterator<Singer> it = singers.iterator();it.hasNext();){
			singerList.add(it.next());
		}
		singerAdapter.getSingerList().addAll(singerList);
		
	}
	
	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected String[] doInBackground(Void... params) {
			return null;
		}

		@Override
		protected void onPostExecute(String[] result) {
			loadMore();
			
			singerAdapter.notifyDataSetChanged();

			// Call onRefreshComplete when the list has been refreshed.
			gvSingers.onRefreshComplete();

			super.onPostExecute(result);
		}
	}

	public SingerAdapter getSingerAdapter() {
		return singerAdapter;
	}

	public void setSingerAdapter(SingerAdapter singerAdapter) {
		this.singerAdapter = singerAdapter;
	}

}
